#!/usr/bin/env python
################################################################################
#
#   wrap_text
# 
#
#
#   Copyright (C) 2007 Leo Goodstadt
#
#   This program is free software; you can redistribute it and/or
#   modify it under the terms of the GNU General Public License
#   as published by the Free Software Foundation; version 2
#   of the License
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#################################################################################

import re
from textwrap import TextWrapper

def wrap_text (message, indent='        ', hanging_indent='  ', width=80):
    """Wraps text using the indent
    
    Doctest: Testing wrap text
    >>> print wrap_text("Some warning messages which spans lines and lines. And goes "
    ...                 "on for ever and ever.\\n"
    ...                 "Can you just take this? Help!\\n"
    ...                 "Some warning messages which spans lines and lines. And goes "
    ...                 "on for ever and ever. Can you just take this? Help"
    ...                 "Some warning messages which spans lines and lines. And goes "
    ...                 "on for ever and ever. Can you just take this? Help",
    ...                 "This is the preamble               ", "  ", 100)
    This is the preamble               Some warning messages which spans lines and lines. And goes on
    This is the preamble                 for ever and ever.
    This is the preamble                 Can you just take this? Help!
    This is the preamble                 Some warning messages which spans lines and lines. And goes on
    This is the preamble                 for ever and ever. Can you just take this? HelpSome warning
    This is the preamble                 messages which spans lines and lines. And goes on for ever and
    This is the preamble                 ever. Can you just take this? Help
    """
    # find tabs or spaces at the beginning of the message
    # these will be preserved
    regex = re.compile(r'^([ \t]*)(.*)', re.DOTALL)
    match = regex.search(message)
    prefix = match.group(1)
    if not prefix:
        prefix = ''
    message = match.group(2)
    
    #
    #   get underlying line breaks which will be preserved
    # 
    message_lines = message.split("\n")
    wrapper = TextWrapper(initial_indent=indent + prefix, 
                  subsequent_indent=indent + prefix + hanging_indent,
                  width =width, 
                  expand_tabs = True)
    message = wrapper.fill(message_lines[0])   
    if len(message_lines) > 1:
        wrapper.initial_indent=indent + prefix + hanging_indent
        for x in xrange(1, len(message_lines)):
            message +=  "\n" + wrapper.fill(message_lines[x])   

    return message
    

def unit_test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    unit_test()

